当前位置: 首页 > 知识库问答 >
问题:

无法为以编程方式创建的WooCommerce产品选择变体

华萧迟
2023-03-14

我正在使用自定义PHP脚本创建WooCommerce产品的变体。用户通过选择字段选择产品的“尺寸”和“颜色”,并基于此创建变体。

工作良好,产品创造与正确的价格,属性和变化。除了你不能选择实际的变体时,尝试购物(他们是“禁用的”)。

当我在WooCommerce的后端打开产品,并在产品上按下蓝色的“更新”按钮时,它就会按照预期工作,并且可以在前端选择和购买变体。

在PHP中,在创建了变体之后,是否有任何“强制更新”产品的功能?

null

下面的代码将颜色和大小作为属性添加到产品中。还创建了一个以“任意颜色”和“任意大小”为规则的单个变体。

function save_wc_custom_attributes($post_id, $custom_attributes) {
$i = 0;
$product = wc_get_product( $post_id );
$product_id = $post_id;
$terms = wp_get_post_terms( $post_id, 'pa_farge') ;
// Remove existing colors from the product
foreach ( $terms as $term ) {
  wp_remove_object_terms( $post_id, $term->term_id, 'pa_farge' );
}
$terms = wp_get_post_terms( $post_id, 'pa_str') ;
// Remove existing sizes from the product
foreach ( $terms as $term ) {
  wp_remove_object_terms( $post_id, $term->term_id, 'pa_str' );
}

foreach ($custom_attributes as $name => $value) {
    // Relate post to a custom attribute, add term if it does not exist
    wp_set_object_terms($post_id, $value, $name, true);
    // Create product attributes array
    $product_attributes[$i] = array(
        'name' => $name, // set attribute name
        'value' => $value, // set attribute value
        'is_visible' => 1,
        'is_variation' => 1,
        'is_taxonomy' => 1
    );
    $i++;
}
// Now update the post with its new attributes
update_post_meta($post_id, '_product_attributes', $product_attributes);
// Details for the variation
$variation_post = array(
    'post_title'  => $product->get_name(),
    'post_name'   => 'product-'.$product_id.'-variation',
    'post_status' => 'publish',
    'post_parent' => $product_id,
    'post_type'   => 'product_variation',
    'guid'        => $product->get_permalink()
);

if( !$product->has_child() ) { 
  // Creating a single variation if none exists
  $variation_id = wp_insert_post( $variation_post );
  $variation = new WC_Product_Variation( $variation_id );
  $variation->set_regular_price( $product->get_price() );
  $variation->save(); // Save the data
  $product_variable = new WC_Product_Variable($post_id);
  $product_variable->sync($post_id);
  wc_delete_product_transients($post_id);
}
else {
  // Update the variation if it already exists
  $product_variable = new WC_Product_Variable($post_id);
  $product_variable->sync($post_id);
  wc_delete_product_transients($post_id);
}
$product->save();
}

共有1个答案

董哲
2023-03-14

null

因此,尝试在代码的末尾添加:

$product = wc_get_product( $post_id );
$product->save();
 类似资料:
  • 我试图以编程方式创建一个订单。使用非常简单: 这符合预期,并且为ID为100的简单产品创建了正确数量的订单。 然而,如果我尝试用一个变体来实现这一点,它的行为似乎并不正确。经过多次尝试和错误,我以这种方式完成了工作: 然而,尽管它确实创建了具有正确产品和正确变化ID的订单,订单的总数总是0(巧合的是,这是第一个变化的价格)。 最初我尝试的是

  • 我在WooCommerce版本3+中创建了一个可变产品(父“产品”)。从一个WordPress插件中,我想以编程方式创建具有新属性值的产品变体(儿童“产品)。 变体属性已在WooCommerce中设置。 因此,每次创建一个变体时,都应以编程方式创建新属性的值,并在父变量product中设置新属性的值。 如何才能做到这一点呢?有可能吗? 更新:我已经为此编写了更多的代码行,并且尝试了许多方法来解决它

  • 我想用PHP代码以编程方式创建一个新的Woocommerce产品,我已经用Woocommerce 3 CRUD方法进行了尝试: 但我得到了这个错误: 22-Oct-2018 20:25:19UTC]PHP致命错误:未捕获错误:类'WC_Product_Simple'未找到 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-con

  • 我有一个WS,它返回非常基本的产品数据:代码、价格和图像。我需要用这些基本数据以编程方式创建Hybris产品,然后进行同步,以便在店面上看到这些产品。 创建具有这些基本信息的产品的步骤是什么?有OOTB服务吗?

  • 如何从插件为WooCommerce创建属性?我只发现: 从这个问题 但这种方法需要某些产品的id。我需要生成一些未附加到任何产品的属性。

  • 我目前正在开发一个网站,它使用WooCommerce和各种扩展(在撰写本文时所有这些都是最新的)来显示一系列属于自定义帖子类型的帖子。 这些帖子是由网站用户在网站前端使用定制的ACF表单创建的。前端表单在帖子类型“shwimmer池”中创建了一个新帖子,旁边还有两个新产品——一个用于订阅,一个用于预订。 这些新帖子的创建在我们的定制类ShwimmerFormHandler中处理。提交池创建表单时激