- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个自定义 WooCommerce 购物车并更新购物车项目的数量工作正常。唯一的问题是它不会自动刷新,只能在页面加载后工作。
我当前的代码,它使用 woocommerce_add_to_cart_fragments
Hook 并使用传入的 $fragments
目的。根据我所看到的,这是正确的方法,但这不起作用,我已经调试过了,对我来说问题似乎是 $fragments
我进入的钩子(Hook)没有我原来的 html,而是一个看起来像这样的 html:
<div class="widget_shopping_cart_content">
<ul class="woocommerce-mini-cart cart_list product_list_widget ">
<li class="woocommerce-mini-cart-item mini_cart_item">
<a href="http://velosity-dev-wordpress.local/cart/?remove_item=92cc227532d17e56e07902b254dfad10&_wpnonce=8ead5f2c51"
class="remove remove_from_cart_button" aria-label="Remove this item" data-product_id="92"
data-cart_item_key="92cc227532d17e56e07902b254dfad10" data-product_sku="">×</a> <a
href="http://velosity-dev-wordpress.local/product/subber-one/">
<img width="300" height="300"
src="http://velosity-dev-wordpress.local/wp-content/uploads/2021/09/mathilde-langevin-WxHJEMUnlIM-unsplash-300x300.jpg"
class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail" alt="" loading="lazy"
srcset="http://velosity-dev-wordpress.local/wp-content/uploads/2021/09/mathilde-langevin-WxHJEMUnlIM-unsplash-300x300.jpg 300w, http://velosity-dev-wordpress.local/wp-content/uploads/2021/09/mathilde-langevin-WxHJEMUnlIM-unsplash-150x150.jpg 150w, http://velosity-dev-wordpress.local/wp-content/uploads/2021/09/mathilde-langevin-WxHJEMUnlIM-unsplash-100x100.jpg 100w"
sizes="(max-width: 300px) 100vw, 300px" />Subber One </a>
<span class="quantity">2 × <span class="woocommerce-Price-amount amount"><bdi><span
class="woocommerce-Price-currencySymbol">$</span>349.00</bdi></span></span>
</li>
</ul>
<p class="woocommerce-mini-cart__total total">
<strong>Subtotal:</strong> <span class="woocommerce-Price-amount amount"><bdi><span
class="woocommerce-Price-currencySymbol">$</span>698.00</bdi></span>
</p>
<p class="woocommerce-mini-cart__buttons buttons"><a href="http://velosity-dev-wordpress.local/cart/"
class="button wc-forward">View cart</a><a href="http://velosity-dev-wordpress.local/checkout/"
class="button checkout wc-forward">Checkout</a></p>
我真的不知道如何将正确的“事件”html 传递给这个钩子(Hook),所以任何指针都将不胜感激!非常感谢提前!
<div class="cart-items" id="cart-items">
<?php foreach(WC()->cart->get_cart() as $cart_item_key => $cart_item) : ?>
<div class="cart-item">
<?php echo $cart_item['data']->get_image(); ?>
<div>
<h3><?php echo $cart_item['data']->get_title() ?></h3>
<div class="quantity">
<input type="number" id="quantity_614459a588590" class="input-text qty text" step="1" min="0" max=""
name="cart[<?php echo $cart_item_key ?>][qty]" value="<?php echo $cart_item['quantity'] ?>"
title="Qty" size="4" placeholder="" inputmode="numeric">
</div>
<div class="price"><?php echo $cart_item['data']->get_price_html() ?></div>
</div>
</div>
<div class="divider"></div>
<? endforeach; ?>
</div>
我的 JS 函数(在 custom.js 中)
jQuery(function ($) {
$(document).on("change", "input.qty", function () {
var $thisbutton = $(this);
var cart_item_key = $(this)
.attr("name")
.replace(/cart\[([\w]+)\]\[qty\]/g, "$1");
var item_quantity = $(this).val();
var currentVal = parseFloat(item_quantity);
$.ajax({
type: "POST",
url: cart_qty_ajax.ajax_url,
data: {
action: "my_cart_qty",
cart_item_key: cart_item_key,
quantity: currentVal,
},
success: function (response) {
jQuery(document.body).trigger("added_to_cart", [response.fragments, response.cart_hash, $thisbutton]);
},
});
});
});
我的 php 函数(在 functions.php 中)
<?php
function enqueue_cart_qty_ajax() {
wp_register_script( 'my_cart_qty-ajax-js', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ), '', true );
wp_localize_script( 'my_cart_qty-ajax-js', 'cart_qty_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'my_cart_qty-ajax-js' );
}
add_action('wp_enqueue_scripts', 'enqueue_cart_qty_ajax');
function ajax_my_cart_qty() {
// Set item key as the hash found in input.qty's name
$cart_item_key = $_POST['cart_item_key'];
$quantity = $_POST['quantity'];
// Get the array of values owned by the product we're updating
$threeball_product_values = WC()->cart->get_cart_item( $cart_item_key );
// Get the quantity of the item in the cart
$threeball_product_quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", '', filter_var($_POST['quantity'], FILTER_SANITIZE_NUMBER_INT)) ), $cart_item_key );
// Update cart validation
$passed_validation = apply_filters( 'woocommerce_update_cart_validation', true, $cart_item_key, $threeball_product_values, $threeball_product_quantity );
// Update the quantity of the item in the cart
if ( $passed_validation ) {
WC()->cart->set_quantity( $cart_item_key, $threeball_product_quantity, true );
}
// Refresh the page
echo do_shortcode( '[woocommerce_cart]' );
die();
}
add_action('wp_ajax_my_cart_qty', 'ajax_my_cart_qty');
add_action('wp_ajax_nopriv_my_cart_qty', 'ajax_my_cart_qty');
//Responsive cart
add_filter( 'woocommerce_add_to_cart_fragments', 'ajaxify_components', 10, 1 );
function ajaxify_components( $fragments ) {
ob_start();
?>
<div class="cart-items" id="cart-items">
<?php foreach(WC()->cart->get_cart() as $cart_item_key => $cart_item) : ?>
<div class="cart-item">
<?php echo $cart_item['data']->get_image(); ?>
<div>
<h3><?php echo $cart_item['data']->get_title() ?></h3>
<div class="quantity">
<input type="number" id="quantity_614459a588590" class="input-text qty text" step="1" min="0" max=""
name="cart[<?php echo $cart_item_key ?>][qty]" value="<?php echo $cart_item['quantity'] ?>"
title="Qty" size="4" placeholder="" inputmode="numeric">
</div>
<div class="price"><?php echo $cart_item['data']->get_price_html() ?></div>
</div>
</div>
<div class="divider"></div>
<? endforeach; ?>
</div>
<?php
$fragments['#cart-items'] = ob_get_clean();
return $fragments;
}
希望有人能给我一个正确方向的指针。谢谢!
最佳答案
我刚刚测试了您的代码,它似乎有效:
echo do_shortcode( '[woocommerce_cart]' );
无法使用 atm,因为它认为购物车是空的(并返回“空购物车”HTML 消息)。但我不明白为什么你需要完整的 woocommerce 购物车 HTML 作为这里的响应。此外,您使用 tour JS 中的响应作为对象,但返回 do_shortcode
将仅返回原始 HTML 字符串。 jQuery(document.body).trigger("added_to_cart")
被调用,触发 Woocommere 购物车片段刷新。由于 AJAX 调用简码响应“错误”,响应对象不正确,但我们仍然可以触发事件。只需确保您的自定义监听器处理“无参数”情况 <? endforeach; ?>
错过了
<?php
.
woocommerce_cart
短代码输出使用 woocommerce 模板。因此,您可以尝试编辑子主题中的 Woocommerce Cart 模板文件以更改 HTML 输出。 (如果您真的需要购物车 HTML 作为对自定义 ajax 调用的响应)。
关于javascript - 从 woocommerce_add_to_cart_fragments 传回的 Woocommerce 错误片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69232916/
我已将重要信息加粗以使其更易于阅读。 我昨天刚刚更新到Xcode 7.3并且一整天都在尝试解决我的问题。对于类(class),我们用 C 编程 并使用 SVN 修改我们所有的文件以创建我们的项目。我使
在互联网上进行了一些挖掘之后,我无法找到一个很好的答案来说明我可以将哪些字符用于 URL 片段。我正在编写一个 javascript 脚本,它将利用 URL 片段。 我想让 URL 看起来不那么复杂,
我正在尝试在分段文件(styp)的 mp4 容器中定位 h264 帧。对于分割,我目前使用 MP4Box dash。我使用 MP4Box 解析器,我注意到在每个关键帧(IDR)中样本的大小与原始文件中
我想要一个自定义片段动画,以便它们淡入右/淡入左,然后在短暂延迟后淡出。假定所有片段都具有类 .visible 和 .current-fragment。我以为我可以在短暂的延迟后删除类 .visibl
有没有人看到过在 C# 中自动调平图像的任何好的片段? 最佳答案 参见 http://code.google.com/p/aforge/ 关于C# Autolevel 片段?,我们在Stack O
如何检索 View 所属的 Fragment/sap.ui.core.Control? BR 克里斯 最佳答案 如果您的控件的标识符包含 View 的标识符(如果您使用的是 XML View ,则类似
我试图了解这个函数的作用。任何人都可以向我解释这一点吗? function newInstance (class) local o = {} setmetatable (o, clas
简介 根据 this documentation可以指定依赖项,包括每个包的版本,如下所示: 问题 需要应用哪个 Nuspec 片段才能安装依赖项的最新版本? 最佳答案 不幸的是,您无法
我有一个 Gatsby 项目,它对两种不同类型的内容进行了非常相似的 GraphQL 查询:常规页面和 wiki 文章。 按蛞蝓 页 export const query = graphql` q
我遇到了以下教程 JSP tricks to make templating easier?用于使用 JSP 创建页面模板(我怎么这么久都没有想到这个?!?)。但是,在进行了一些搜索之后,我似乎无法弄
我是 Django 的新手,我试图找出如何将 HTML 片段与模型相关联。 我的 HTML 片段只是一个 div。我想重用那个 div(你可以把它想象成一个缩略图) 情况是这样的:在我的主页中,我想显
我经常使用 vim,但我的工作流程通常迫使我与其他 IDE 交互,所以我不是一个像上帝一样的 vim super 用户,我也不想很快成为。 Vim 不是我的 IDE,我也不希望它是。这是一款快速轻便的
我刚刚了解到一个关于在抛出错误时执行 Javascript 的重要事实。在我开始对此下结论之前,我最好验证一下我是否正确。 给定一个包含 2 个脚本的 HTML 页面: 脚本1: doSometh
我是在Chrome片段中编写的: let myVar = someValue; 当我尝试第二次运行它时,它说该变量已被声明并在第一行引发错误。 错误是: Uncaught SyntaxError: I
我想要两个像素着色器;首先要做一件事,然后再做其他事情。这是可能的,还是我必须将所有内容打包到一个着色器中? 最佳答案 您可以这样做,例如通过从主入口点对在各种着色器对象中实现的函数进行函数调用。 m
我正在尝试检查汽车前面是否有任何障碍物。假设汽车在位置“2”。我的目标是检查位置“3”处是否有障碍物。 可能没有明确的障碍事实,这意味着在特定位置没有障碍。我检查使用是否存在有条件。但是在规则 r6
我想在文本区域内编写一个 JavaScript,而不运行 JavaScript。显示为一些可复制的文本。 我使用 jquery 同时插入文本区域和代码片段: $("#copy-snippet-cont
有人可以解释以下 htacess 行,我理解部分内容,但想要更深入的知识。作为注释,我假设它按预期工作,这目前还没有上线,我只是在阅读一些工作簿,这是打印的。 // Don't understand
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我目前正在尝试使用 Jquery 根据下拉列表的值附加音频标签 html 列表。主要问题是,当选择值更改时,empty() 和append() 方法根本不会将html 注入(inject)到播放列表d
我是一名优秀的程序员,十分优秀!