【问题标题】:Woocommerce - Displaying variations inside the loopWoocommerce - 在循环内显示变化
【发布时间】:2012-11-18 15:59:01
【问题描述】:
<?php

    switch ( $product->product_type ) {
        case "variable" :
            $link   = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
            $label  = apply_filters( 'variable_add_to_cart_text', __('Select options', 'woocommerce') );
        break;
        case "grouped" :
            $link   = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
            $label  = apply_filters( 'grouped_add_to_cart_text', __('View options', 'woocommerce') );
        break;
        case "external" :
            $link   = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
            $label  = apply_filters( 'external_add_to_cart_text', __('Read More', 'woocommerce') );
        break;
        default :
            $link   = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
            $label  = apply_filters( 'add_to_cart_text', __('Add to cart', 'woocommerce') );
        break;
    }

    printf('<a href="%s" rel="nofollow" data-product_id="%s" class="add_to_cart_button button product_type_%s">%s</a>', $link, $product->id, $product->product_type, $label);

?>

我正在尝试在循环中显示变体,以便客户可以从商店页面将可变产品添加到他们的购物车(请参见下面的屏幕截图)...

http://cl.ly/image/42401k0X0X2I

我知道我需要包含该功能-

get_available_variations();

我很确定这已经返回了一个数组,它只是将该数组放入一个选择下拉列表中 + 列出变体(S、M、L、XL)并有一个链接将该变体添加到购物篮中。

干杯!

【问题讨论】:

  • 最上面的代码哪里来的?
  • woocommerce/templates/loop/add-to-cart.php

标签: php wordpress variables loops woocommerce


【解决方案1】:

单个帖子页面的变体下拉模板文件位于此处: woocommerce\templates\single-product\add-to-cart\variable.php

这需要以下脚本来传递产品变量信息:

<script type="text/javascript">
var product_variations_<?php echo $post->ID; ?> = <?php echo json_encode( $available_variations ) ?>;
</script>

以及以下隐藏字段:

<input type="hidden" name="variation_id" value="" /> - where the value is the variation ID

我希望这是其他人可以帮助建立的一个开始。

【讨论】:

  • 我把上面的 sn-p 放在了我的loop/price.php 文件中。但$available_variations 在我这边输出null。还有什么我忘了包括的吗?
【解决方案2】:

我在尝试解决相同问题时发现了您的帖子。终于找到了……

function woocommerce_variable_add_to_cart() {
    global $product;

    // Enqueue variation scripts
    wp_enqueue_script( 'wc-add-to-cart-variation' );

    // Load the template
    woocommerce_get_template( 'single-product/add-to-cart/variable.php', array(
            'available_variations'  => $product->get_available_variations(),
            'attributes'            => $product->get_variation_attributes(),
            'selected_attributes'   => $product->get_variation_default_attributes()
        ) );
}
}

woocommerce-template.php

这在 loop/add-to-cart.php 中对我有用

switch ( $product->product_type ) {
        case "variable" :
            $link   = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
            $label  = woocommerce_variable_add_to_cart();
        break;

如果这有帮助,请告诉我:)

【讨论】:

    【解决方案3】:

    我在Remi Corson's blog 上找到了一种简单的方法。
    使用以下代码更改购物车按钮的 href 值:

    http://example.com/cart/?add-to-cart=[PRODUCT_ID]&variation_id=[VARIATION_ID]&attribute_pa_[ATTRIBUTE_SLUG]=[ATTRIBUTE_SLUG_VALUE]
    

    例子:

    http://example.com/cart/?add-to-cart=123&variation_id=456&attribute_pa_colour=black
    

    使用get_available_variations(); 函数很容易得到变化值。产品ID可以使用get_the_ID();函数。

    【讨论】:

    • 当我将$id = get_the_ID(); $variations = get_available_variations($id); 添加到我的自定义单页模板(在我创建的 woocommece/..... 的主题文件夹中)时出现致命错误:调用未定义的函数 get_available_variations()。 @Guilherme Marconi 有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多