【发布时间】:2017-06-28 14:13:02
【问题描述】:
我在 Wordpress 中进行的自定义构建有一个非常奇怪的问题。我正在使用挂钩来覆盖显示产品的自定义页面上的入门主题的“添加到购物车”按钮。奇怪的是,当我循环通过添加到购物车按钮为我的产品添加数量选项时,原来的 Ajax 功能消失了。然后我实现了另一个功能将其重新添加(并导致我的自定义“查看购物车”按钮的购物车中的商品编号更新),但虽然它在购物车中工作,但它似乎不适用于我的自定义商店页面.
我在标题中使用这个 sn-p 来处理购物车内容:
<?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中的两个函数:
/**
* 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;
}
我认为我添加新的添加到购物车按钮的第二个函数会覆盖最初的 Ajax 功能,但是我尝试重新添加此功能的所有操作都不起作用。我在 JS/jQuery 方面不是最好的,所以可能是我没有正确实现我的代码。
对此的任何帮助将不胜感激。
【问题讨论】:
标签: php jquery ajax wordpress woocommerce