能否請讓我知道如何以編程方式在WooCommerce中添加自定義產品屬性和變量?我知道這很容易在Woocommerce GUI完成,但我需要通過functions.php以編程方式在WooCommerce中添加自定義產品屬性和變量
這樣做我有一個屬性被稱爲「類」和一些變量作爲[A,B,C,D]現在我想將它們添加到購物車和購物網頁,我alraedy試過這個功能,但它沒有添加任何東西到頁面:
function woocommerce_variable_add_to_cart() {
//Type attribute
$product_attributes['type'] = array(
//Make sure the 'name' is same as you have the attribute
'name' => htmlspecialchars(stripslashes('Class')),
'value' => $attributes,
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
//Add as post meta
update_post_meta($post_id, '_product_attributes', $product_attributes);
}
感謝
任何一個是否有任何想法這個? – Behseini
我以前做過這個,雖然我不能發佈代碼(NDA),但我可以告訴你,你需要使用'woocommerce_cart_loaded_from_session','woocommerce_before_cart'和(如果使用AJAX)'woocommerce_ajax_added_to_cart'習慣如果您使用AJAX,使用'$ woocommerce-> session'和'$ woocommerce-> cart'在兩個位置更新。當以編程方式調整購物車項目中的屬性時,這些都是必需的。 – Dez
http://wordpress.stackexchange.com/questions/124758/adding-custom-product-attribute-and-variable-in-woocommerce-programatically – GavinR