将频率添加到Woocommerce订阅

时间:2020-08-31 13:47:50

标签: wordpress woocommerce

我正在寻找开发人员来帮助我在Woocommerce订阅中添加选择频率订阅产品变化属性,请参阅链接-> https://staging-terravidaonline.kinsta.cloud/product/cbd-tattoo/

我开发了此代码,该代码允许在常规产品上显示订阅产品,并可以在两个产品之间以半径切换。我们本来想添加频率功能,但是由于某种原因,它没有将产品添加到购物车并显示错误消息。

由于某种原因,正确的产品未添加到购物车并显示错误消息:请选择产品选项… 为什么会发生?

如有任何疑问,请告诉我

感谢一百万

查看代码:

function example_add_subscription_option() {
    global $product;
    $display = false;
    $linked_id = $product->get_meta( 'linked_subscription_id' );
    $attribute_name = 'Frequency';
    $user_id        = get_current_user_id();  
    /*$product_attribute = wc_display_product_attributes($product); */

if ( isset( $linked_id ) &&  $linked_id ) {
    $linked_product = wc_get_product( $linked_id[0] );
    // If linked object is an actual product, set $display to true
    $attributes = $linked_product->get_attribute( 'pa_frequency' );

    if ( is_object( $linked_product ) ) {
        $display = true;
    }
}
$attributes = explode(", ", $attributes);
// Display the subscription toggle markup
if ( $display ) :
    $onetime = array(
        'id' => $product->get_id(),
        'label' => 'One Time Order',
        'button' => 'Add to Cart',
        'price_html' => $product->get_price_html()
    );
    $subscription = array(
        'id' => $linked_product->get_id(),
        'label' => '30% Off First Order',
        'button' => 'Sign for AUTOSHIP',
        'price_html' => $linked_product->get_price_html()
    );
    ?>
    <div class="subscription-toggle">
        <div class="radio-select">
            <img class="autoship-logo" src="https://terravidaonline.com/wp-content/uploads/AUTOSHIP-LOGO-FINAL-1.png" width="80" height="60">
            <div>
                <div class="option option-onetime-order" data-product="<?php echo $onetime['id']; ?>" data-button="<?php echo $onetime['button']; ?>" data-price="<?php echo esc_html( $onetime['price_html'] ); ?>">
                    <input class="sub-switch" checked="checked" name="onetime-order" type="radio" />
                    <label for="onetime-order"><?php echo $onetime['label']; ?></label>
                </div>
                <div class="option option-subscription" data-product="<?php echo $subscription['id']; ?>" data-button="<?php echo $subscription['button']; ?>" data-price="<?php echo esc_html( $subscription['price_html'] ); ?>">
                    <input class="sub-switch" name="subscription-order" type="radio" />
                    <label name="subscription-order"><?php echo $subscription['label']; ?></label>
                </div>
            </div>
        </div>
      
  <table class="variations" cellspacing="0" hidden>
            <tbody>
      <tr>
                    <td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label></td>
                    <td class="value">
                        <?php  
          $selected =  $linked_product->get_default_attributes( $attribute_name );
          //$attribute_name = 'pa' . $attribute_name;
          wc_dropdown_variation_attribute_options(array('options' => $attributes,'attribute' => $attribute_name, 'product' => $linked_product, 'selected' => $selected['pa_frequency']));?>
        </td>
                </tr>
            </tbody>
        </table>
  <input type="hidden" name="variation_id" class="variation_id" value="<?php echo esc_attr( $post->ID ); ?>">
    </div>

    <script>
    jQuery(document).ready(function($) {
    $(".option-onetime-order").click(function () {
 $(".variations").hide("fast")


});
    //To show the table
     $(".option-subscription").click(function () {
     $(".variations").show(2000);
   });    
            $('.subscription-toggle .option').on( 'click', function() {
                $('.subscription-toggle input').prop('checked', false);
                var $option = $(this).find('input');
                var $product = $(this).data('product');
                var button = $(this).data('button');
                var price = $(this).data('price');
                $option.prop('checked', true);
                $('.single_add_to_cart_button').text(button);
                $('.price').html(price);
                $('.single_add_to_cart_button').attr("value", $product);             
            });
        });
        </script> 
    <?php endif ; ?> 
<?php
}
add_shortcode('subscription', 'example_add_subscription_option');

0 个答案:

没有答案