首页 > 社区问答列表 >使用复选框添加产品选项卡到Woocommerce,以添加功能来添加第二个“添加到购物车”。

  使用复选框添加产品选项卡到Woocommerce,以添加功能来添加第二个“添加到购物车”。

我需要一些您的帮助,非常感谢您的努力!

我的问题是,安装的插件替换了产品页面上的标准添加到购物车按钮。我不想改变它,并且我需要在其下方添加第二个添加到购物车按钮。问题是,另一个用于免费产品样品的插件需要常规的添加到购物车按钮。

当我添加全局脚本时,它在所有产品上都有效。


Add_action( 'woocommerce_product_meta_start', 'woocommerce_template_single_add_to_cart, 1 );

我希望将此功能添加到产品选项卡中,并且可以通过复选框在所选产品上启用或禁用。

到目前为止,我的代码没有起到任何作用。复选框可以工作,并且复选框的保存也正常。但是代码片段没有在产品前端页面执行添加到购物车的命令。


// Add checkbox field to the Product tab in the admin area

function add_checkbox_to_product_tab() {

    // Add checkbox field to the General tab

    woocommerce_wp_checkbox( array(

        'id' => 'add_to_cart_checkbox',

        'label' => 'Warenkorb Hinzufügen',

                'desc_tip'       => false, // true or false, show description directly or as tooltip

        'description' => 'ja'

    ) );

}

add_action( 'woocommerce_product_options_general_product_data', 'add_checkbox_to_product_tab' );

 

// Save checkbox field value

function save_checkbox_value( $product ) {

    $checkbox = isset( $_POST['add_to_cart_checkbox'] ) ? 'yes' : 'no';

    $product->update_meta_data( 'add_to_cart_checkbox', $checkbox );

}

add_action( 'woocommerce_admin_process_product_object', 'save_checkbox_value' );

 

// Add action when checkbox is selected with 'yes'

function add_action_when_checkbox_selected( $product_id ) {

    $checkbox_value = get_post_meta( $product_id, '_add_to_cart_checkbox', true );

    if ( $checkbox_value == 'yes' ) {

        do_action( 'woocommerce_product_meta_start' );

        do_action( 'woocommerce_template_single_add_to_cart' );

    }

}

add_action( 'woocommerce_template_single_add_to_cart', 'add_action_when_checkbox_selected',  1 );


P粉262113569
P粉262113569

  • P粉543344381
  • P粉543344381   采纳为最佳   2023-08-01 11:44:52 1楼

    您可以使用`woocommerce_product_meta_start`钩子。请查看下面的代码。

    // Add a checkbox field to the Product tab in the admin area
    function add_checkbox_to_product_tab() {
        // Add checkbox field to the General tab
        woocommerce_wp_checkbox( array(
            'id' => 'add_to_cart_checkbox',
            'label' => 'Warenkorb Hinzufügen',
            'desc_tip' => false,
            'description' => 'ja'
        ) );
    }
    add_action( 'woocommerce_product_options_general_product_data', 'add_checkbox_to_product_tab' );
    
    // Save checkbox field value
    function save_checkbox_value( $product ) {
        $checkbox = isset( $_POST['add_to_cart_checkbox'] ) ? 'yes' : 'no';
        $product->update_meta_data( 'add_to_cart_checkbox', $checkbox );
    }
    add_action( 'woocommerce_admin_process_product_object', 'save_checkbox_value' );
    
    // Remove standard "Add to Cart" button if checkbox value is 'no'
    function add_custom_add_to_cart_button() {
        global $product;
        $checkbox_value = $product->get_meta( 'add_to_cart_checkbox' );
        if ( $checkbox_value == 'yes' ) {
            // Add your custom "Add to Cart" button code here
            echo '<a href="' . esc_url( $product->add_to_cart_url() ) . '" class="button alt">Custom Add to Cart</a>';
        }
    }
    add_action( 'woocommerce_product_meta_start', 'add_custom_add_to_cart_button', 40 );

    经过测试,代码有效。

    +0 添加回复