【问题标题】:Add linked buttons to variations of variable product In Woocommerce archives在 Woocommerce 档案中将链接按钮添加到可变产品的变体
【发布时间】:2019-04-19 13:47:55
【问题描述】:

在 woocommerce 中,我正在建立一个销售复古地图的网站。我需要找到一种方法,使用按钮将产品 ID 和变体 ID 从 woocommerce 商店传递到单个产品页面,以获得两个单独的变体,而无需将产品添加到购物车。

有两种产品类型,原始地图(显然只有一种)和无限的数字打印。

我通过创建具有“原始”和“数字印刷”属性的可变产品来区分这些。每个属性都有一个变化,所以很简单。

公司希望这两个选项(“原始”和“数字印刷”)作为按钮出现在 woocommerce 商店首页。

这个想法是,“原版”按钮只有在原版有库存时才会出现(同样,实现起来很简单)。

问题从这里开始:

这个想法是,当按下这些按钮中的任何一个时,它不会将产品添加到购物车,而是会转到相应的单个产品页面,在那里,根据变体类型,将有一系列选项选择地图是否加框和安装,如果是数字的,打印应该有多大等等等等。选择后,可以使用单个产品页面上的添加到购物车按钮将产品添加到购物车。

目前我似乎无法将信息发送到单个产品页面而不将其添加到购物车(!)

我当前使用的代码使用 woocommerce_after_shop_loop_item 和 woocommerce_single_variation 来覆盖默认的添加到购物车按钮并将其替换为以下内容,但显然这仍然是将产品添加到购物车。查看代码,它很冗长,而且显然过于复杂,但我很难过。添加到购物车按钮需要替换为简单地移动到单个产品页面并使用产品 ID 和变体 ID 的东西,但我不知道是什么。从理论上讲,您可以将它序列化为一个数组并作为附加到锚点的 $_GET 发送,但除了这可能导致的潜在安全问题之外,它在工具栏中看起来很劣质。

所以,总而言之。我需要找到一种方法,使用按钮将产品 id 和变体 id 从 woocommerce 商店传递到单个产品页面,以获得两个单独的变体,而无需将产品添加到购物车中。有什么想法吗?

代码如下:

function mc_loop_variation_add_to_cart_button() {
global $product;

$product_obj = new WC_Product_Factory();
$product = $product_obj->get_product($product);   

if ($product->product_type == 'variable'):
  $children   = $product->get_children( $args = '', $output = OBJECT ); 

  foreach ($children as $key=>$value) {

      $product_variatons = new WC_Product_Variation($value);

      if ( $product_variatons->exists() && $product_variatons->variation_is_visible() ) {
          $variations[$value] = $product_variatons->get_variation_attributes();

          foreach ($variations[$value] as $key=>$value) {
            $stock = $product_variatons->get_stock_quantity();
            $product_price = $product_variatons->regular_price;



            if($value=="original") {

              if($stock > 0) {
                echo "Price: £" . $product_price;

            ?>

              <div class="woocommerce-variation-add-to-cart variations_button">
                <?php

                ?>
                  <button type="submit" class="single_add_to_cart_button button">Buy <?php //echo $value; ?></button>
                  <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
                  <input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
                  <input type="hidden" name="variation_id" class="variation_id" value="<?php echo $value; ?>" />
              </div>

                <?php
                } else {
                  echo "";
                }

            } else {
                echo "Price: £" . $product_price;
                ?>
              <div class="woocommerce-variation-add-to-cart variations_button">
                  <button type="submit" class="single_add_to_cart_button button">Buy <?php echo $value; ?></button>
                  <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
                  <input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
                  <input type="hidden" name="variation_id" class="variation_id" value="<?php echo $value; ?>" />
              </div>
                <?php
            }
          ?>



          <?php
          }
      }
  }
endif;

}

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy variations


    【解决方案1】:

    对于具有特定属性的可变产品,以作为商店的档案页面变体,以下代码将:

    • 移除默认价格范围
    • 删除添加到购物车
    • 添加 2 个按钮,价格与产品变体相关联

    按钮链接会将客户带到可变产品中的正确预选变体。

    您需要在第一个函数中定义具有'original''digital-prints' 术语值的产品属性。您需要在第二个函数中定义产品属性术语 slugs。

    对于印刷品,我们以Price from: $123.00 之类的形式显示最低价格。

    代码:

    add_action( 'woocommerce_after_shop_loop_item_title', 'loop_custom_variable_products', 2 );
    function loop_custom_variable_products() {
        global $product;
    
        // HERE define the product attribute (for 'original' and 'digital-prints' term values
        $attribute = 'type'; // <====  <====  <====  <====  <====  <====  <====  <====  <====
    
        // Only variable products
        if ( $product->get_type() === 'variable' && $product->get_attribute($attribute) ) :
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); // Remove price
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); // remove add to cart
            add_action( 'woocommerce_after_shop_loop_item', 'loop_variations_custom_buttons_and_prices', 10 ); // Add prices with custom buttons
        endif;
    }
    
    function loop_variations_custom_buttons_and_prices() {
        global $product;
    
        if ( $product->get_type() === 'variable' ) :
    
        // HERE define your targeted slugs for the defined product attribute
        $targeted_slugs = array('original', 'digital-prints'); // <====  <====  <====  <====
    
        $found_original = $found_a_print = false;
        $data = $url_var = [];
    
        $min_price_html = __("Price from: ") . strip_tags( wc_price( $product->get_variation_price( 'min', true ) ) );
    
        // Loop through product variations ids
        foreach ($product->get_children() as $variation_id ){
    
            // Get the WC_Product_Variation Object
            $variation = wc_get_product( $variation_id );
    
            if ( $variation->is_type('variation') && $variation->variation_is_visible() && $variation->variation_is_active() ) {
                $stock_qty  = $variation->get_stock_quantity();
                $price_html = __("Price: "); strip_tags( wc_price( wc_get_price_to_display( $variation ) ) );
                $attributes = $variation->get_variation_attributes();
    
                // Loop through variation attributes
                foreach ( $attributes as $attribute => $term_slug ) {
                    if( $term_slug === $targeted_slugs[0] ) {
                        $data[$term_slug]['price'] = $price_html . strip_tags( wc_price( wc_get_price_to_display( $variation ) ) );
                        $data[$term_slug]['stock'] = $stock_qty > 0 ? true : false;
    
                        $found_original = true;
                    }
                    if( $term_slug === $targeted_slugs[1] ) {
                        $data[$term_slug]['price'] = $min_price_html;
                        $data[$term_slug]['stock'] = true;
    
                        $targeted_attribute = $attribute;
    
                        $found_a_print = true;
                    }
                }
            }
            if( $found_original && $found_a_print) {
                // If both are found we stop the main loop
                break;
            }
        }
    
        // Output prices and buttons
        foreach( $targeted_slugs as $slug ) {
            if( isset($data[$slug]) && $data[$slug]['stock'] ) {
                echo '<div class="variation-' . $slug . '">
                    <div class="price" style="margin-bottom:14px;">' . $data[$slug]['price'] . '</div>';
                foreach( $attributes as $key => $value ){
                    if( isset($targeted_attribute) && $targeted_attribute === $key ) {
                        $url_var[] = $key . '=' . $slug;
                    } else {
                        $url_var[] = $key . '=null';
                    }
                }
                $href = $product->get_permalink() . '?' . implode('&', $url_var);
                echo '<a href="'.$href.'" class="button">' . __ ("Buy") . ' ' . str_replace('-', ' ', $slug)  . '</a>
                </div>';
            }
        }
        endif;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    你会得到类似的东西:

    【讨论】:

    • 感谢 Loic - 我实际上使用您对另一个问题的回答设计了一个部分解决方案:stackoverflow.com/questions/49027614/… 但这更整洁,并且填补了我自己知识中的一些(许多)空白,所以,干杯 ;-)
    • 现在已经用细齿梳完成了这个。有几处我不明白 为什么 他们工作。首先,$targeted_attribute = $attribute; 怎么来的?只在第二个条件中设置但仍然在第一个条件中有效?这是 foreach 循环的副作用吗?即,如果没有在别处定义,它取最后一个设定值?其次,$attributes = $variation->get_variation_attributes();仅在第一个 foreach 循环中设置。我原以为循环的范围意味着必须在第二个中再次设置它,但显然不是?无需回答更多,只是大声思考
    • @Stef 解释:1)由于您没有给出问题中涉及的主要属性,我使用$targeted_attribute = $attribute; 来捕获主要涉及的属性...... 2)基于@的第二个foreach循环987654329@ 允许为按钮链接设置正确的 url 变量。
    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2019-10-21
    • 2018-08-31
    • 2022-01-07
    • 2015-05-09
    • 2019-07-16
    相关资源
    最近更新 更多