php - WooCommerce - 自动添加奖励产品

标签 php wordpress woocommerce

我想制作一个脚本来自动将奖励产品添加到 WooCommerce 购物车。

添加特定产品后,奖励产品将被添加到购物车中。但我从未见过任何具有功能的改进且完全工作的代码 - 例如自动删除奖励项目或从购物车中删除主要产品。

在此解决方案中,我提出了以下代码,该代码具有以下功能:

  • 选项
  • 多个必需的产品
  • 自动添加
  • 自动删除(如果购物车中没有所需产品)


function bonus_product() {
  if (is_admin()) return;

  //## OPTIONS
  $options = (object) array(
      'bonus_product_id'      => 1891,  //bonus product to add
      'required_products_id'  => array(1873), //at least on of the specific product(s) needs to be represented in the cart
    );

  //function variables
  $cart_items = WC()->cart->get_cart();
  $bonus_product_found    = false;
  $required_product_found = false;

  //check if the cart is not empty
  if(sizeof($cart_items) > 0) {
    //checking for required products. loop through the cart items
    foreach ($cart_items as $key => $item) {
      //bonus product already in the cart?
      if($item['product_id'] == $options->bonus_product_id) {
        $bonus_product_found = true;
      }
      //one of required products in the cart?
      if(in_array($item['product_id'], $options->required_products_id)) {
        $required_product_found = true;
      }
    }

    //adding/removing bonus product
    //add bonus product to the cart
    if(!$bonus_product_found && $required_product_found) {
      WC()->cart->add_to_cart($options->bonus_product_id);  
    }
    //remove bonus product from the cart if none of required items is in the cart
    if($bonus_product_found && !$required_product_found) {
      $cart = WC()->instance()->cart;
      $cart_id = $cart->generate_cart_id($options->bonus_product_id);
      $cart_item_id = $cart->find_product_in_cart($cart_id);
      $cart->set_quantity($cart_item_id, 0);
    }      
  }   
}

add_action( 'init', 'bonus_product' );

最佳答案

我已经编写了一个替代版本,基于添加到购物车从购物车操作,这似乎更合适。

$bonus_options = (object) array(
  'bonus_product_id'      => 1891,
  'required_products_id'  => array( 1873 )
);

// this function called whenever there is a product added to cart
function add_bonus_product( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    global $bonus_options;
    // is the product is eligible for bonus product?
    if( in_array( $product_id, $bonus_options->required_products_id) ) {
        // add the bonus product to cart
        WC()->cart->add_to_cart( $bonus_options->bonus_product_id, 1, 0, array(), array( "parent_product_line_item" => $cart_item_key ) );
        // later if user removes the product from cart we can use the "parent_product_line_item" to remove the bonus product as well
    }
}
add_action( 'woocommerce_add_to_cart', 'add_bonus_product', 10, 6 );

// this function will be called whenever there is a product removed from cart
function remove_bonus_product( $cart_item_key, $cart ) {
    $cart_items = WC()->cart->get_cart();
    foreach ( $cart_items as $key => $item ) {
        if( $item["parent_product_line_item"] == $cart_item_key ) {
            // ok this cart item is a bonus item to the product that being removed from the cart
            // So remove this too
            WC()->cart->remove_cart_item( $key );
        }
    }
}

add_action( 'woocommerce_cart_item_removed', 'remove_bonus_product', 10, 2 );

关于php - WooCommerce - 自动添加奖励产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213650/

相关文章:

php - 如何获取 Woocommerce 电子邮件通知中的 cookie 值?

php - 如何跟踪 PHP 中的变量变化

php - 在 PHP MySQL 搜索中返回 5 个结果

php - 当我将查询结果导出到 Excel CSV 时,始终显示 HTML 代码

php - stub 单个函数 - 不涉及类 - php

wordpress - 我想通过 htaccess 或使用 WordPress 功能更改 WordPress 网站特定页面的 URL

php - 模型属性的翻译

jquery - 绝对定位固定定位 div IE 内的元素不起作用

wordpress - 在没有缩略图的 Woocommerce Wordpress 插件中显示产品类别列表

wordpress - 使用社交登录 (OAuth) 验证自定义 WP API 端点