自定义产品上缺少“添加到购物车”按钮

扎特加(Admir Zatega)

码:

<?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

有谁知道去哪里看?谢谢 :)

埃米利奥·韦尼加斯(Emilio Venegas)

我为此苦苦挣扎了一段时间,最终解决的问题是在我的“ 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 );
}

然后在“ woocommerce_single_product_summary”操作中手动添加带有$ product-> add_to_cart_url()作为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' );
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据用户国家和产品 ID 自定义添加到购物车按钮

在可变的单个产品页面中自定义添加到购物车按钮/链接

单个Woocommerce产品自定义变体添加到购物车按钮

根据WooCommerce产品自定义库存状态禁用添加到购物车按钮

在Woocommerce中的产品自定义循环上启用Ajax添加到购物车按钮

自定义添加到购物车按钮以将多个产品添加到购物车中,数量为:woocommerce

将产品添加到Woocommerce单个产品页面上的购物车后,激活自定义按钮

自定义 WooCoomerce 产品页面,添加到购物车按钮重定向到产品页面

根据产品类型,WooCommerce的“添加到购物车”按钮旁边的“自定义”按钮

通过WooCommerce产品设置中的自定义复选框禁用添加到购物车按钮

附加的自定义按钮,可在添加到Wovcommerce中的购物车之前更改产品价格

在WooCommerce中为特定产品类别自定义“添加到购物车”按钮

自定义特定 WooCommerce 产品类别上的“添加到购物车”按钮

WooCommerce自定义产品类型-多个添加到购物车部分的问题

使用自定义信息和价格将产品添加到购物车

将自定义产品计算价格添加到Woocommerce购物车

将可变产品的自定义属性添加到WooCommerce中的购物车

Woocommerce 产品添加到购物车自定义数据 img

使用woocommerce上的自定义按钮短代码替换添加到购物车按钮

将多个自定义数据数组添加到WooCommerce自定义Ajax上的购物车项目中添加到购物车

如何在WooCommerce中重定向到自定义添加到购物车按钮上的结帐

根据自定义管理复选框的条件更改商店档案上的“添加到购物车”按钮

将基于ACF字段的自定义“添加到购物车”按钮添加到WooCommerce单个产品页面

WooCommerce自定义PHP代码更改添加到购物车按钮文本

自定义添加到购物车按钮Shopify

在“添加到购物车”按钮下显示自定义字段

在Woocommerce商店页面上自定义添加到购物车按钮

在 WooCommerce 中添加到购物车后更改自定义 Ajax 添加到购物车按钮文本

扩展“添加到购物车”功能以在Sylius的自定义表中添加产品变体