【问题标题】:Add a product to woocommerce cart if other cart items are in a specific category如果其他购物车项目属于特定类别,则将产品添加到 woocommerce 购物车
【发布时间】:2015-11-25 22:09:12
【问题描述】:

如果购物车中的任何商品来自特定类别,我需要能够有条件地仅将 一个 产品添加到 WooCommerce 订单/购物车中。 这是我尝试过的代码,但它锁定了结帐和购物车页面,不返回任何 woocommerce 信息,或之后的任何内容(没有页脚,没有额外的页面元素等......直到前面的 WooCommerce 数据为止) -end,后面有一个很大的空格)。请,如果有人能指出我正确的方向,将不胜感激!

这是我的 functions.php 文件中的内容:

        /**
 * Checks the cart to verify whether or not a product from a Category is in the cart
 * @param $category Accepts the Product Category Name, ID, Slug or array of them
 * @return bool
 */
function qualifies_basedon_product_category( $category ) {
    foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
        if( has_term( $category, 'fee', get_id_from_product( $product_in_cart, false ) ) ) {
            return true;
        }
    }
    // Return false in case anything fails
    return false;
}

/**
 * Adds a specific product to the cart
 * @param $product_id Product to be added to the cart
 */
function add_incentive_to_cart( $product_id ) {
    // Check the cart for this product
    $cart_id = WC()->cart->generate_cart_id( $product_id );
    $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
    // Add the product only if it's not in the cart already
    if( ! $prod_in_cart ) {
        WC()->cart->add_to_cart( $product_id );
    }
}



add_action( 'woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() {
    // Incentive product
    $incentive_product_id = 506;

    if( qualifies_basedon_product_category( 'fee' ) ) {
        add_incentive_to_cart( $incentive_product_id );
    } 
}

【问题讨论】:

  • 这与我想要实现的目标“相似”,但使用的是产品而不是费用......stackoverflow.com/questions/26240591/… 另外,在有人建议之前,我已经购买了 Woo Force Sells 扩展,但问题是它会自动为该类别中的每个产品添加额外的产品,而不仅仅是每个购物车一次......所以如果有另一种使用强制销售的方法来处理它,我也会对此持开放态度...... .只需能够将强制出售的商品在购物车中出现的次数限制为一次。

标签: wordpress woocommerce


【解决方案1】:

您可以调查以下代码-

add_action('woocommerce_add_to_cart', 'my_woocommerce_add_to_cart', 10, 6);

function my_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    $incentive_product_id = 506;
    $category = 'fee';
    $taxonomy = 'fee';

    if( has_term( $category, $taxonomy, $product_id ) ){    
        $cart_id = WC()->cart->generate_cart_id( $incentive_product_id );
        $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );

        // Add the product only if it's not in the cart already
        if( ! $prod_in_cart ) {
            WC()->cart->add_to_cart( $incentive_product_id );
        }
    }
}

希望这对你有帮助。

【讨论】:

  • 非常感谢您。我试过了,但它给了我一个内部服务器错误......在这一点上,我决定放弃这个想法并以不同的方式处理它,但我非常感谢你的努力和花时间尝试和帮助!
  • 上面的代码有一个拼写错误,我错过了第一个IF语句的右括号。现在这段代码应该可以正常工作了。如果不是它不起作用,你可以在这里通知它,因为我可能会学到新东西吗?谢谢。
【解决方案2】:

把它放在你的functions.php中:

function df_add_ticket_surcharge( $cart_object ) {
global $woocommerce;
$specialfeecat = 20763; // category id you want to check for
$spfee = 0.00; // initialize special fee
$spfeeperprod = 0.00; //special fee per product
$found = $false; //required to make the code later only add the item once, also stop loops.
$product_id = 38607; //product id of the one you want to add

foreach ( $cart_object->cart_contents as $key => $value ) {

    $proid = $value['product_id']; //get the product id from cart
    $quantiy = $value['quantity']; //get quantity from cart
    $itmprice = $value['data']->price; //get product price

//check if the desired product is already in cart.
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->id == $product_id )
                $found = true;
        }
    }

//check for the category and add desired product
    $terms = get_the_terms( $proid, 'product_cat' ); //get taxonomy of the products
    if ( $terms && ! is_wp_error( $terms ) ) :
        foreach ( $terms as $term ) {
            $catid = $term->term_id;
            if($specialfeecat == $catid && ! $found) {
                WC()->cart->add_to_cart( 38607 );
            }
        }
    endif;  
   }
}
add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' );

我从现有代码修改了代码,这就是为什么 spfee 和 spfeeperprod 仍然存在但您并不真正需要它们的原因,这就是我将它们设置为 0 的原因。我注意到使此代码工作缺少什么这部分是:

if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->id == $product_id )
                $found = true;
        }
    }

这将检查您要添加的项目的产品 ID,该产品 ID 指定为“$product_id”。希望这会有所帮助(我昨天为此苦苦挣扎,但今天早上终于取得了突破!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多