Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WooCommerce中为特定产品类别的购物车项目自动添加产品_Php_Wordpress_Woocommerce_Product_Cart - Fatal编程技术网

Php 在WooCommerce中为特定产品类别的购物车项目自动添加产品

Php 在WooCommerce中为特定产品类别的购物车项目自动添加产品,php,wordpress,woocommerce,product,cart,Php,Wordpress,Woocommerce,Product,Cart,我有一个代码,可以将产品(押金)自动添加到客户购物车中,不管他选择了什么产品——下面的代码在functions.php中。这个很好用 但是现在,如何扩展这样一个代码,即当客户从特定的产品类别中选择了一种产品时,该产品才会自动添加到购物车中?例如,当客户购买礼品卡时,不应将押金添加到购物车中 非常感谢 /** * Automatically adds product to cart on visit */ add_action( 'template_redirect', 'add_produc

我有一个代码,可以将产品(押金)自动添加到客户购物车中,不管他选择了什么产品——下面的代码在functions.php中。这个很好用

但是现在,如何扩展这样一个代码,即当客户从特定的产品类别中选择了一种产品时,该产品才会自动添加到购物车中?例如,当客户购买礼品卡时,不应将押金添加到购物车中

非常感谢

/**
 * Automatically adds product to cart on visit
 */
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 1267; //product added automatically
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->get_id() == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {

        }
    }
}

您必须以完全不同的方式使用Wordpress条件函数hast_term()

如果产品类别中的产品已在购物车中,则以下代码将自动添加到购物车中预定义的产品:

add_action( 'woocommerce_before_calculate_totals', 'auto_add_item_based_on_product_category', 10, 1 );
function auto_add_item_based_on_product_category( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    $required_categories = array('t-shirts'); // Required product category(ies)
    $added_product_id = 1267; // Specific product to be added automatically
    $matched_category = false;

    // Loop through cart items
    foreach ( $cart->get_cart() as $item_key => $item ) {
        // Check for product category
        if( has_term( $required_categories, 'product_cat', $item['product_id'] ) ) {
            $matched_category = true;
        }
        // Check if specific product is already auto added
        if( $item['data']->get_id() == $added_product_id ) {
            $saved_item_key = $item_key; // keep cart item key
        }
    }

    // If specific product is already auto added but without items from product category
    if ( isset($saved_item_key) && ! $matched_category ) {
        $cart->remove_cart_item( $saved_item_key ); // Remove specific product
    }
    // If there is an item from defined product category and specific product is not in cart
    elseif ( ! isset($saved_item_key) && $matched_category ) {
        $cart->add_to_cart( $added_product_id ); // Add specific product
    }
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作