【问题标题】:woocoomerce attribute with value and variable assign to category具有值和变量分配给类别的 woocommerce 属性
【发布时间】:2018-04-03 06:41:10
【问题描述】:

我正在尝试以编程方式为可变产品创建两个产品属性“宽度”和“高度”:

  • “宽度”产品属性将有 13 个值 (24,30,36.....96)
  • “身高”产品属性将有 18 个值 (18,24,30....120)

因此,可变产品将具有 总变体:
13(宽度)x 18(高度)= 234 个变体,具有不同的价格值,应在属于特定产品类别的产品的前端自动创建(自动填充)。

例如(在单个产品页面中)用户选择时:

  • “宽度”下拉菜单中的选项值 24
  • “高度”下拉菜单中的选项值 18

此版本的价格应为 38 美元

用户选择时同理:

  • 宽度 = 30
  • 身高 = 18
  • 价格 = 45 美元

有人给我提供了代码,但作为新手无法弄清楚代码:

//this will assign attribute and variable to category "ABC" only
  $target_products = array(
     'post_type' => 'product',
     'post_status' => 'publish',
     'product_cat' => 'ABC',
     'posts_per_page'=>-1
);

// This will display attribute and variable auto fill in front end
$variation_data =  array(
    'attributes' => array(
        'width'  => '24',
        'height' => '18',
    ),
    'sku'           => '',
    'regular_price' => '38.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);

$variation_data =  array(
    'attributes' => array(
        'width'  => '30',
        'height' => '18',
    ),
    'sku'           => '',
    'regular_price' => '45.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);


$variation_data =  array(
    'attributes' => array(
        'width'  => '36',
        'height' => '18',
    ),
    'sku'           => '',
    'regular_price' => '52.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);

有人告诉我,上面的代码可以完成这项工作,当用户在创建新产品时从特定产品类别(“ABC”)中选择产品时,将创建属性宽度和高度以及 234 变量(我不知道'没有手动创建属性和变量)

【问题讨论】:

  • 您应该尝试改写您的问题,因为它不是很清楚。您还应该尝试仅保留必要的内容。此外,我们不明白您需要什么帮助……
  • 你可以在图片中看到宽度和高度以及价格,我想要的是当用户选择类别“ABC”时,这个宽度和高度属性应该在前端创建,价格值在选择时自动属于“ABC”类
  • @LoicTheAztec 对我的问题进行了更改并直接指出了我的需求

标签: php woocommerce hook-woocommerce


【解决方案1】:

我找到了链接,上面写着 Programmatically Add an Attribute in Woocommerce to a Product code i 如下所示,但它没有在创建新产品时添加属性(将此代码放在 function.php 中)

  $target_products = array(
     'post_type' => 'product',
     'post_status' => 'publish',
     'product_cat' => 'Living Room Light Balls',
     'posts_per_page'=>-1
);

$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {

   while ($my_query->have_posts()) : $my_query->the_post(); 

     $term_taxonomy_ids = wp_set_object_terms( get_the_ID(), '0.65m', 'pa_diameter', true );
     $thedata = Array('pa_diameter'=>Array(
       'name'=>'pa_diameter',
       'value'=>'0.65m',
       'is_visible' => '1',
       'is_taxonomy' => '1'
     ));
     update_post_meta( get_the_ID(),'_product_attributes',$thedata); 

   endwhile;
}

wp_reset_query();

【讨论】:

猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 2013-05-09
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多