gpt4 book ai didi

php - 显示 WooCommerce 可变产品选项

转载 作者:行者123 更新时间:2023-12-01 05:42:51 25 4
gpt4 key购买 nike

我正在尝试创建一个查询来显示产品及其一些信息。

我坚持展示他们的可变产品选项(它们都将是可变产品)。之后,我必须创建一个“添加到购物车”按钮,该按钮将添加到购物车,生成成功或错误消息,并让用户继续购物。一旦用户的购物车中有东西,我想显示一个结帐按钮,将他们带到结帐页面。

这是我到目前为止所得到的

<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
// Indicate the product category - in this case "training"
'product_cat' => 'training'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();

// Show the product Featured Image and gallery images
woocommerce_show_product_images();

// Show the product Title
echo the_title();

// Show the product Custom Fields
echo get_post_meta($post->ID, 'Description', true);
echo get_post_meta($post->ID, 'Specifications', true);
echo get_post_meta($post->ID, 'Video', true);

// Show the Product Price
echo $product->get_price_html();

// Show the Variable Product options, for example: Color, Size etc
// stuck here!

// Show the Add to cart button
// stuck here!

// Show the Checkout button if there is an item in the users cart
// stuck here!

endwhile;
}
wp_reset_postdata();
?>

有谁知道我应该使用什么功能来显示此信息?我很难在文档/谷歌上找到任何有关它的信息。

谢谢大家!!!

最佳答案

Woocommerce 默认附带可变产品选项。为此,您首先必须创建属性。如果所有产品都具有相同的属性,您可以创建全局,也可以为每个产品单独创建属性。

这是我正在工作的网站的屏幕截图。我们有 4 种不同的套餐。所以我的属性是“packages”,里面有4个包。 enter image description here

保存属性后,转到下一个名为“变体”的选项卡。选择包属性,它将自动加载所有变体。这是另一个屏幕截图。您必须选择默认变体。 enter image description here

完成上述所有更改后,您可以单击任何变体并更改产品参数,例如价格、图像等。 enter image description here

变体可用于衬衫等产品,您可以拥有不同颜色的衬衫,尽管衬衫的价格相同,但您可以让用户选择颜色并查看衬衫在该颜色下的外观。

虽然 woocommerce 默认情况下已经设置了所有内容,但如果您要更改默认的 WordPress 模板,那么最好的方法是从 woocommerce 复制 templates 文件夹,并将此文件夹以 woocommerce 的名称放置在您的事件主题中,这样就可以了类似于 wp-content/themes/theme-name/woocommerce。在这些文件中,您需要 woocommerce 模板文件夹中的所有文件和文件夹。 Woocommerce 将自动选择所有这些文件并为其提供服务。 woocommerce 建议使用此方法,因为直接更改插件将在下一次插件更新时丢失所有自定义设置。

编辑您的情况:抱歉,我想我得意忘形了:p。无论如何,这取决于您所处的情况以及您用来获取这些值的文件,但是下面的代码将输出所有产品变体以及该变体中的所有属性。我还没有测试过,可能需要微调。 **

global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
echo 'variation ID'.$value['variation_id'];
foreach ($value['attributes'] as $attr_key => $attr_value) {
echo $attr_key.': '.$attr_value;
}
}

关于php - 显示 WooCommerce 可变产品选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com