私はWordpressでやっているカスタムビルドでかなり奇妙な問題があります。私は、製品を表示するカスタムページ上のスターターテーマの「カートに追加」ボタンを上書きするフックを使用しています。奇妙なことは、商品に数量オプションを追加するためにカートに追加ボタンをループすると、元のAjax関数が消えてしまうことです。私はそれを元に戻して(そして、カスタム 'ビューカート'ボタンのアイテムインカート番号を更新させる)別の機能を実装しましたが、カート内で動作しますが、私のカスタムショップページでは機能していないようです。Wordpress - WoocommerceでAjaxを使用してカートに追加ボタンのショートカット
/**
* Ensure cart contents update when products are added to the cart via AJAX
*/
function my_header_add_to_cart_fragment($fragments) {
ob_start();
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart'); ?>"><?php
if ($count > 0) {
?>
<span class="cart-contents-count"><?php echo esc_html($count); ?></span>
<?php
}
?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter('woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment');
/**
* Add quantity to products in Products Page
*/
add_filter('woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2);
function quantity_inputs_for_woocommerce_loop_add_to_cart_link($html, $product) {
if ($product && $product->is_type('simple') && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually()) {
$html = '<form action="' . esc_url($product->add_to_cart_url()) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input(array(), $product, false);
$html .= '<button type="submit" class="button alt">' . esc_html($product->add_to_cart_text()) . '</button>';
$html .= '</form>';
}
return $html;
}
I:ここ
<?php if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart'); ?>"><?php
if ($count > 0) {
?>
<span class="cart-contents-count"><?php echo esc_html($count); ?></span>
<?php
}
?></a>
そして、私の子供のテーマのfunctions.phpの私の二つの機能です:私はカートの内容を処理するために、私のヘッダーにこのスニペットを使用しています
私の2番目の機能が新しいadd-to-cartボタンを追加すると、最初のAjax機能が上書きされていると思うが、この機能を追加するために何をしようとしても機能していない。私はJS/jQueryで最善ではないので、おそらく私は自分のコードを適切に実装していないだろう。
これについてのお手伝いがあれば幸いです。
ありがとう、私はクラスを追加しようとしましたが、それは動作していません。ループにフィルタとして適用しようとしましたが、ボタンに追加されたHTMLクラスとして追加しようとしました...ここに何か不足していますか? – user1192309