php - 在 Woocommerce 的购物车中自动添加或删除免费赠品产品

标签 php wordpress woocommerce cart product

我经营着一家 WooCommerce 商店,我们希望为每位顾客提供一本免费赠品(电子书),在您将产品添加到购物篮后,该赠品将显示在购物篮中。

例子:
您将“product1”添加到购物篮,购物篮现在将显示 2 件产品。 “产品 1”和“免费赠品”。 当您从购物篮中取出产品时,赠品将再次被移除。

我现在得到了这段代码:

add_action( 'woocommerce_add_to_cart', 'check_freebie_exists', 10, 6 );
function check_freebie_exists($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    if($product_id == 1234) { // obviously replace this with the product that triggers the freebie
        /* or you could use
        if(has_term($cat_id, 'product_cat', $product_id)) { to check if a product is in a particular product_cat
        or you could check anything else related to the product
        */
        $hasfreebie = false;
        // loop through the cart to check the freebie is not already there
        global $woocommerce;
        $cart = $woocommerce->cart->get_cart();
        foreach($cart as $key => $values) {
            if($values['data']->id == $your_freebie_product_id) {
                $hasfreebie = true;
                break;
            }
        }
        if(!$hasfreebie) {
            $woocommerce->cart->add_to_cart($your_freebie_product_id);
        }
    }
}

add_action( 'woocommerce_cart_item_removed', 'remove_freebie', 10, 2 );
function remove_freebie( $cart_item_key, $cart ) {
    $hasmaster = false;
    $freebiekey = NULL;
    foreach($cart as $key => $values) {
        if($values['data']->id == 1234) { // check that we have the product that should trigger the freebie
            $hasmaster = true;
        } elseif($values['data']->id == $your_freebie_product_id) {
            $freebiekey = $key;
        }
    }
    if(!$hasmaster && $freebiekey) {
        $cart->remove_cart_item($freebiekey);
    }
}

但它似乎还没有正常工作。

我做错了什么?

任何帮助将不胜感激。

最佳答案

Update 2 - October 2018 - Improved and enhanced code (Completely revisited)

以下代码将在首次添加到购物车时仅添加一次免费赠品。如果删除所有其他购物车项目,则免费赠品也将被删除:

add_action( 'woocommerce_before_calculate_totals', 'add_remove_freebie', 50, 1 );
function add_remove_freebie( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $freebie_id = 70; // <== HERE set the freebie product ID
    $has_others = false;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Added Woocommerce compatibility version
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();

        if( $product_id == $freebie_id ) {
            // Freebie is in cart
            $freebie_key = $cart_item_key;
        } else {
            // Other items are in cart
            $has_others = true;
        }
    }
    // If freebie product is alone in cart we remove it
    if( ! $has_others && isset( $freebie_key ) ){
        $cart->remove_cart_item( $freebie_key );
    } elseif ( $has_others && ! isset( $freebie_key ) ) {
        $cart->add_to_cart($freebie_id);
    }
}

代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。

关于php - 在 Woocommerce 的购物车中自动添加或删除免费赠品产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234409/

相关文章:

php - 如何获取数据并插入到同一数据库中的另一个表中?

php - nginx 验证到 mysql 后端

Azure 上的 Wordpress,504,媒体无法上传,无法删除插件

javascript - 在 WordPress 站点内使用 hubspot 表单时缺少 jquery

apache - htaccess : 301 redirect removing part of url

php - 在 Woocommerce 中的产品自定义循环上启用 Ajax 添加到购物车按钮

javascript - 如何将 js 表单元格值 POST 到 PHP 并插入到 MYSQL 数据库

javascript - 响应式 Adwords 广告

mysql - SQL 查询 - 将值添加到多个唯一 ID 中

wordpress - 对archive-product.php所做的更改不起作用