मैं कस्टम विशेषताओं को जोड़ने के लिए इस कोड का उपयोग कर रहा हूँ
$attributes = array(
array("name"=>"Size","options"=>array("S","L","XL","XXL"),"position"=>1,"visible"=>1,"variation"=>1),
array("name"=>"Color","options"=>array("Red","Blue","Black","White"),"position"=>2,"visible"=>1,"variation"=>1)
);
if($attributes){
$productAttributes=array();
foreach($attributes as $attribute){
$attr = wc_sanitize_taxonomy_name(stripslashes($attribute["name"])); // remove any unwanted chars and return the valid string for taxonomy name
$attr = 'pa_'.$attr; // woocommerce prepend pa_ to each attribute name
if($attribute["options"]){
foreach($attribute["options"] as $option){
wp_set_object_terms($product_id,$option,$attr,true); // save the possible option value for the attribute which will be used for variation later
}
}
$productAttributes[sanitize_title($attr)] = array(
'name' => sanitize_title($attr),
'value' => $attribute["options"],
'position' => $attribute["position"],
'is_visible' => $attribute["visible"],
'is_variation' => $attribute["variation"],
'is_taxonomy' => '1'
);
}
update_post_meta(11874,'_product_attributes',$productAttributes); // save the meta entry for product attributes
इस कोड का परिणाम मुझे केवल उत्पाद विशेषता नाम बिना टर्म वैल्यू के जोड़ा गया है ...
मैंने इसके बारे में बहुत खोजा लेकिन कोई जवाब नहीं मिला।
1 उत्तर
आपके कोड में कुछ गलतियाँ हैं। आपकी मुख्य गलती: उत्पाद मेटा डेटा के रूप में सहेजने के लिए विशेषता शब्द (अंत में) शब्द आईडी की एक सरणी (शब्द नामों के बजाय) होना चाहिए।
निम्नलिखित पुनरीक्षित कोड का प्रयास करें:
$product_id = 11874;
$attributes_data = array(
array('name'=>'Size', 'options'=>array('S', 'L', 'XL', 'XXL'), 'visible' => 1, 'variation' => 1 ),
array('name'=>'Color', 'options'=>array('Red', 'Blue', 'Black', 'White'), 'visible' => 1, 'variation' => 1 )
);
if( sizeof($attributes_data) > 0 ){
$attributes = array(); // Initializing
// Loop through defined attribute data
foreach( $attributes_data as $key => $attribute_array ) {
if( isset($attribute_array['name']) && isset($attribute_array['options']) ){
// Clean attribute name to get the taxonomy
$taxonomy = 'pa_' . wc_sanitize_taxonomy_name( $attribute_array['name'] );
$option_term_ids = array(); // Initializing
// Loop through defined attribute data options (terms values)
foreach( $attribute_array['options'] as $option ){
if( term_exists( $option, $taxonomy ) ){
// Save the possible option value for the attribute which will be used for variation later
wp_set_object_terms( $product_id, $option, $taxonomy, true );
// Get the term ID
$option_term_ids[] = get_term_by( 'name', $option, $taxonomy )->term_id;
}
}
}
// Loop through defined attribute data
$attributes[$taxonomy] = array(
'name' => $taxonomy,
'value' => $option_term_ids, // Need to be term IDs
'position' => $key + 1,
'is_visible' => $attribute_array['visible'],
'is_variation' => $attribute_array['variation'],
'is_taxonomy' => '1'
);
}
// Save the meta entry for product attributes
update_post_meta( $product_id, '_product_attributes', $attributes );
}
परीक्षण और काम करता है।
नोट: सभी उत्पाद विशेषताओं और उनके मूल्यों को परिभाषित करने की आवश्यकता है (आपके वास्तविक कोड के साथ)
संबंधित: Woocommerce में प्रोग्रामेटिक रूप से नई उत्पाद विशेषता बनाएं
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
php
PHP एक व्यापक रूप से उपयोग किया जाता है, उच्च-स्तरीय, गतिशील, वस्तु-उन्मुख, और व्याख्या की गई स्क्रिप्टिंग भाषा मुख्य रूप से सर्वर-साइड वेब विकास के लिए डिज़ाइन की गई है। PHP भाषा के बारे में सवालों के लिए इस्तेमाल किया।