问题描述
我正在尝试使用此功能将产品添加到我的购物车:
WC()->cart->add_to_cart( $id,$quantity=1,$variation_id );
但是,它仅在购物车为空时有效。如果购物车中已有产品,则不会添加新产品。
出于测试目的,我使用了这样的表单按钮:
<form name="addpro" method="post" action="">
<input type="submit" name="addcustomcarts" value="ADD TOO CART">
</form>
add_action('init','customcart');
function customcart() {
if (isset($_POST["addcustomcarts"])) {
// global $woocommerce;
//Create main product
$product = new WC_Product_Variable();
$product->set_name("Tdwo");
//Create the attribute object
$attribute = new WC_Product_Attribute();
//pa_size tax id
$attribute->set_id( 0 ); // -> SET to 0
//pa_size slug
$attribute->set_name( 'size' ); // -> removed 'pa_' prefix
//Set terms slugs
$attribute->set_options( array(
'blue','grey'
) );
$attribute->set_position( 0 );
//If enabled
$attribute->set_visible( 1 );
//If we are going to use attribute in order to generate variations
$attribute->set_variation( 1 );
$product->set_attributes(array($attribute));
//Save main product to get its id
$id = $product->save();
$variation = new WC_Product_Variation();
$variation->set_regular_price(10);
$variation->set_parent_id($id);
//Set attributes requires a key/value containing
// tax and term slug
$variation->set_attributes(array(
'size' => 'blue' // -> removed 'pa_' prefix
));
//Save variation,returns variation id
$variation_id = $variation->save() ;
echo $variation_id;
// echo get_permalink( $id ); // -> returns a link to check the newly created product
WC()->cart->add_to_cart( $id,$variation_id );
exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
}
}
解决方法
尝试以下(清理一下您的代码并将属性数组添加到 add_to_cart()
方法):
add_action('init','customcart');
function customcart() {
if (isset($_POST["addcustomcarts"])) {
## -- Settings -- ##
$variable_product_name = "Tdwo"; // Main variable product name
$attribute_name_slug = 'size'; // Define attriburte name (slug) to be used for variations
$attribute_terms_slugs = array( 'blue','grey'); // Define attriburte term slugs to be used with variations
// Get an empty instance of the WC_Product_Variable Object
$product = new WC_Product_Variable();
$product->set_name($variable_product_name);
// Get an empty instance of the WC_Product_Attribute Object
$attribute = new WC_Product_Attribute();
$attribute->set_id( 0 ); // Set attribute Id (First,set to 0)
$attribute->set_name( $attribute_name_slug ); // Set attribute name
$attribute->set_options( $attribute_terms_slugs ); // Set attribute term slugs to be used with variations
$attribute->set_position( 0 ); // Position: First one,set to 0
$attribute->set_visible( 1 ); // Visible on product page
$attribute->set_variation( 1 ); // Used for variations
$product->set_attributes( array($attribute) );
// Save main product to get its id
$product_id = $product->save();
// --------------------------------
## -- Settings -- ##
$chosen_term_slug = 'blue'; // Set chosen attribute term slug for the current variation
$variation_price = 10; // The variation regular price
$variation = new WC_Product_Variation();
$variation->set_parent_id( $product_id );
$variation->set_regular_price( $variation_price );
$variation->set_price( $variation_price );
// Set attributes requires a key/value containing
$variation->set_attributes( array( $attribute_name_slug => $attribute_term_slug ) );
//Save variation,returns variation id
$variation_id = $variation->save();
// Attribute formatted name slug and term slug for add to cart
$attributes = array( 'attribute_pa_' . $attribute_name => $attribute_term_slug );
WC()->cart->add_to_cart( $product_id,1,$variation_id,$attributes,array() );
exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。它可以工作。