0 レビュー
1 回答
php-WoocommerceでAJAXを使用してミニカートを更新/更新します
このコードをWooCommerceのセットアップに追加しようとしています。このコードは、PHPを配置する場所にショッピングカートのリンクを追加し、カート内のアイテムをAJAXで変更すると更新します:https://docs.woocommerce。 com / document / show-cart-contents-total /
スニペットは次のとおりです:
HTML-PHP:
<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
functions.php
ファイル内:
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
}
しかし、AJAXは機能していません。 2番目のスニペットはfunctions.phpに追加する必要があるすべてですか?
関数を定義するだけでなく、関数を呼び出す必要があるように感じますか?
または、AJAXを機能させるには、一般的に何らかの方法でAJAXをアクティブ化する必要がありますか?
わからない
0
レビュー
答え :
解決策:
フィルターフックwoocommerce_add_to_cart_fragments
が関数にありません…
それを機能させるには、次のようにする必要があります:
add_filter('woocommerce_add_to_cart_fragments'、'header_add_to_cart_fragment'、30、1);
関数header_add_to_cart_fragment($ Fragments){
グローバル$woocommerce;
ob_start();
?>
<?php echo sprintf(_n('%d item'、'%d items'、$ woocommerce-> cart-> cart_contents_count、'woothemes')、$ woocommerce-> cart-> cart_contents_count);?>-<?php echo $ woocommerce-> cart-> get_cart_total(); ?>
<?php
$ Fragments ['a.cart-customlocation'] = ob_get_clean();
$fragmentsを返します。
}
コードは、アクティブな子テーマ(またはアクティブなテーマ)のfunction.phpファイルに入ります。未テスト。
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。