【问题标题】:Missing Add to Cart button on custom product自定义产品上缺少“添加到购物车”按钮
【发布时间】:2018-06-03 07:06:35
【问题描述】:

代码:

<?php

// --- Product Type: re_subscription -- Custom Fields: type (mls / nomls), duration in days ---

// --- Actions & Filters ---

add_filter('product_type_selector', 're_add_subscription_product_type');
add_filter('woocommerce_product_data_tabs', 're_remove_shipping_menu', 10, 1);
add_filter('woocommerce_product_class', 're_subscription_product_class', 10, 2);

add_action('plugins_loaded', 're_create_subscription_product_type');
add_action('admin_footer', 're_subscription_admin_custom_js');
add_action('woocommerce_product_options_general_product_data', 're_add_custom_settings');
add_action('woocommerce_process_product_meta', 're_save_custom_settings');


// --- Create Product Type ---

function re_add_subscription_product_type($types){
    $types['re_subscription'] = __('RealEstate Subscription');
    return $types;
}

function re_create_subscription_product_type(){
     // declare the product class
     class WC_Product_Re_Subscription extends WC_Product{
        public function __construct($product) {
           $this->product_type = 're_subscription';
           parent::__construct($product);
           // add additional functions here
        }

        /**
         * Get internal type.
         * Needed for WooCommerce 3.0 Compatibility
         * @return string
         */

        public function get_type() {
            return 're_subscription';
        }
    }
}

function re_subscription_product_class($classname, $product_type) {
    if ($product_type == 're_subscription')
        $classname = 'WC_Product_Re_Subscription';

    return $classname;
}


// --- Remove Shipping Menu ---

function re_remove_shipping_menu($tabs){
    /*
        The available tab array keys are:
        general, inventory, shipping, linked_product, attribute, variations, advanced
     */
    unset($tabs['shipping']);
    return $tabs;
}

// --- Show General Tab ---

function re_subscription_admin_custom_js() {
    if ('product' != get_post_type()) :
        return;
    endif;

    ?>
    <script type='text/javascript'>
        jQuery(document).ready(function () {
            // For Price tab
            jQuery('.product_data_tabs .general_tab').show();
            jQuery('#general_product_data .pricing').addClass('show_if_re_subscription').show();

           });
    </script>
    <?php
}

// --- Add Fields ---

function re_add_custom_settings() {
    global $woocommerce, $post;
    $post_id = $post->ID;
    $re_is_mls = get_post_meta($post_id, 're_is_mls', true);
    if($re_is_mls == '')
        $re_is_mls = 'no';

    echo '<div class="options_group show_if_re_subscription">';
    // Duration
    woocommerce_wp_text_input(
      array(
       'id'                => 're_duration_field',
       'label'             => __('Duration in days', 'woocommerce' ),
       'placeholder'       => '30',
       'desc_tip'    => 'true',
       'description'       => __('Duration of the subscription in days', 'woocommerce'),
       'type'              => 'number',
       ));

    // Type
    woocommerce_wp_checkbox(
       array(
       'id'            => 're_is_mls',
       'label'         => __('Is this MLS?', 'woocommerce'),
       'value' => $re_is_mls,
    ));

    echo '</div>';
}

function re_save_custom_settings($post_id){
    // Save Duration field
    $re_duration_fielld = $_POST['re_duration_field'];
    if(!empty($re_duration_fielld))
        update_post_meta($post_id, 're_duration_field', esc_attr($re_duration_fielld));

    // Save MLS option
    $re_is_mls = isset($_POST['re_is_mls']) ? 'yes' : 'no';
    update_post_meta($post_id, 're_is_mls', $re_is_mls);
}

截图:http://prntscr.com/hq4zzo

在简单的产品上,添加到购物车产品按钮显示并且可以正常工作,但在我的自定义产品上,添加到购物车按钮丢失。价格仍然显示得很好。

我已经发布到 /r/woocommerce 并没有得到任何答案:/希望在这里得到一些东西:3

有人知道去哪里看吗?谢谢:)

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    类似于Howdy_McGee's answer,但调用do_action而不是wc_get_template

    add_action( "woocommerce_{$my_product_type}_add_to_cart", function() {
        do_action( 'woocommerce_simple_add_to_cart' );
    });
    

    【讨论】:

    • 这在产品页面上效果很好,但在循环页面上不起作用。任何想法如何在自定义产品类型的循环页面上启用“添加到购物车”?
    【解决方案2】:

    WooCommerce 按产品类型 获取他们的添加到购物车模板。在 WooCommerce 插件模板文件夹中,我们看到:

    plugins\woocommerce\templates\single-product\add-to-cart
    

    在这里,我们有一个包含基于产品类型的 HTML 的模板。您可以使用返回简单产品模板的类型创建一个函数、调用和操作。

    /**
     * Gives custom product type a template
     *
     * @return void
     */
    function se47910821_answer() {
        wc_get_template( 'single-product/add-to-cart/simple.php' );
    }
    add_action( 'woocommerce_re_subscription_add_to_cart', 'se47910821_answer' );
    

    钩子是:

    add_action( "woocommerce_{$product_type}_add_to_cart", 'callback_function' );
    

    【讨论】:

    • 谢谢,正是我想要的 :) +1
    【解决方案3】:

    我为此苦苦挣扎了一段时间,最终修复它的是在我的“WC_Product_Custom”类中添加一个“add_to_cart_url()”方法,如下所示:

    public function add_to_cart_url() {
        $url = $this->is_purchasable() && $this->is_in_stock() ? remove_query_arg( 'added-to-cart', add_query_arg( 'add-to-cart', $this->id ) ) : get_permalink( $this->id );
    
        return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
    }
    

    然后手动添加一个链接 $product->add_to_cart_url() 作为“woocommerce_single_product_summary”操作中的href,如下所示:

    add_action( 'woocommerce_single_product_summary', 'custom_product_add_to_cart', 60 );
    function custom_product_add_to_cart () {
        global $product;
    
        // Make sure it's our custom product type
        if ( 'my_custom_product' == $product->get_type() ) {
            do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    
            <p class="cart">
                <a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" rel="nofollow" class="single_add_to_cart_button button alt">
                    <?php echo "Add to cart"; ?>
                </a>
            </p>
    
            <?php do_action( 'woocommerce_after_add_to_cart_button' );
        }
    }
    

    【讨论】:

      【解决方案4】:

      补充,因为之前的答案在循环中不起作用(例如商店和存档产品页面),正如Bruno Crosier 所指出的那样。 而且我还添加了对购物车按钮的 ajax 支持。 您必须在您的自定义产品类中添加从 class-wc-product-simple.php 复制的这些功能(当然您可以自定义它):

      public function __construct($product) {
          $this->product_type = your_product_name;
          $this->supports[]   = 'ajax_add_to_cart';
          parent::__construct( $product );
      }
      public function add_to_cart_url() {
                  $url = $this->is_purchasable() && $this->is_in_stock() ? remove_query_arg(
                      'added-to-cart',
                      add_query_arg(
                          array(
                              'add-to-cart' => $this->get_id(),
                          ),
                          ( function_exists( 'is_feed' ) && is_feed() ) || ( function_exists( 'is_404' ) && is_404() ) ? $this->get_permalink() : ''
                      )
                  ) : $this->get_permalink();
                  return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
              }
      
              /**
               * Get the add to cart button text.
               *
               * @return string
               */
              public function add_to_cart_text() {
                  $text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
      
                  return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
              }
      
              public function add_to_cart_description() {
              /* translators: %s: Product title */
              $text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add &ldquo;%s&rdquo; to your cart', 'woocommerce' ) : __( 'Read more about &ldquo;%s&rdquo;', 'woocommerce' );
      
              return apply_filters( 'woocommerce_product_add_to_cart_description', sprintf( $text, $this->get_name() ), $this );
              }
      

      【讨论】:

        猜你喜欢
        • 2023-01-18
        • 2020-09-01
        • 2015-12-04
        • 1970-01-01
        • 2019-12-13
        • 2019-07-16
        • 1970-01-01
        • 2015-05-24
        • 1970-01-01
        相关资源
        最近更新 更多