当前位置: 首页 > 知识库问答 >
问题:

woocommerce钩子从购物车中删除产品

刘昌翰
2023-03-14

你在为它做什么?我在文件末尾(wp-content/plugins/woocommerce/woocommerce.php)写了这个字符串:

add_filter('woocommerce_before_cart_item_quantity_zero', 'wordpress_before_cart_item_quantity_zero', 10, 1);

function wordpress_before_cart_item_quantity_zero($item) {
    global $wpdb;
    global $woocommerce;

    $cart = $woocommerce->cart;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $id_product = $cart_item['product_id'];
        $wpdb->prepare("DELETE FROM wp_block_product WHERE product_id = %d", $id_product);
        $preparing_query = $wpdb->query($preparing_query);
    }

    var_dump($wpdb);
    exit;
}

但当我从购物车中删除项目时,它就不起作用了!

add_action('woocommerce_before_cart_item_quantity_zero', 'wordpress_before_cart_item_quantity_zero');

但也不起作用

共有1个答案

狄冠宇
2023-03-14

根据源代码,您要查找的是'woocommerce_cart_item_removed',它在从购物车中移除一个项目时运行:

function so31115243_after_remove_product($cart_item_key) {
    // Your custom function
}
add_action( 'woocommerce_cart_item_removed', 'so31115243_after_remove_product' );
 类似资料: