[英]add product to cart manually in woocommerce
need to add product to cart programatically with custom parameters 需要使用自定义参数以编程方式将产品添加到购物车
I used woocommerce_add_cart_item_data
this hook to use custom item data in woocommerce session 我使用
woocommerce_add_cart_item_data
这个钩子在woocommerce会话中使用自定义项目数据
function wdm_add_item_data($cart_item_data,$product_id)
{
/*Here, We are adding item in WooCommerce session with, wdm_user_custom_data_value name*/
global $woocommerce;
$new_value = array("option1" => $_REQUEST['option1'], "option2" => $_REQUEST['option2'], );
if(empty($cart_item_data))
return $new_value;
else
return array_merge($cart_item_data,$new_value);
//Unset our custom session variable, as it is no longer needed.
}
$woocommerce->cart->add_to_cart(16); i tried this code
how to pass custom parameters in this function? 如何在此函数中传递自定义参数?
I am using this code. 我正在使用此代码。
add_action('init', 'products_to_cart');
function products_to_cart(){
$woocommerce->cart->add_to_cart($prod_ID, $prod_Qty);
}
This is what eventually got it working for me. 这就是最终使它为我工作的原因。 Couldn't find many resources online so I'm posting this.
在网上找不到很多资源,因此我将其发布。
I added this code to the functions.php This will fire every time any page is loaded, but you could put a condition around it. 我将此代码添加到functions.php中,这将在每次加载任何页面时触发,但是您可以在其周围放置一个条件。
add_action('wp_loaded', function() {
global $woocommerce;
$sku = 'ABC101';
$quantity = 20;
$product_id = wc_get_product_id_by_sku($sku);
$woocommerce->cart->add_to_cart($product_id, $quantity);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.