从商店页面上的购物车中删除商品

时间:2020-04-01 18:25:33

标签: php jquery ajax button woocommerce

我正在尝试在商店页面上插入“从购物车中删除产品”按钮(我与woocommerce合作) 我想我快要这样做了,但什么都没用

这是我添加按钮的代码(在woocommerce的文件add-to-cart.php中)

useEffect(() => {
    if (containerRef.current) {
      containerRef.current.focus();
    }
 }, [containerRef.current]);

有我的function.php代码:

echo apply_filters(
    'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
    sprintf(
        '<a href="%s" class="remove_item_button">supprimer</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
        esc_html( $product->add_to_cart_text() )
    ),
    $product,
    $args
);

还有我的JS:

function remove_item_from_cart() {
    $cart_item_key = $_POST['cart_item_key'];
    if($cart_item_key){
       WC()->cart->remove_cart_item($cart_item_key);
       return true;
    }
    return false;
}
add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');

如果有人可以帮助我:)

0 个答案:

没有答案