douyi3833 2012-10-18 16:28
浏览 22

WooCommerce,如果选中自定义Meta复选框,则将产品退回“售罄”

having a bit of trouble with WooCommerce, basically on the store I am working on there will need to be an option to display a "Call to Order" message instead of showing the default "Add to Cart" form or "Sold Out" message.

I have kind of achieved this by adding a custom metabox checkbox in the product edit back-end, and an if statement in the single product add to cart template, the only problem is all this does is hide the add to cart form and the product can still be added like so "http://localhost/wordpress/woocommerce/shop/?add-to-cart=14" so its not exactly full-proof.

What I am looking for is to somehow make the product out of stock when the custom checkbox is checked, I was looking in the WooCommerce plugin admin back-end files and this is used a lot:

update_post_meta( $post_id, '_stock_status', 'outofstock' );

So I thought it would be as easy this in functons.php:

global $post;
if ($meta_box = get_post_meta($post->ID, "_cmb_test_checkbox", true) ) :
update_post_meta( $post_id, '_stock_status', 'outofstock' );
endif;

Turns out its not. Any help would be greatly appreciated as this is driving me crazy.

Cheers!

  • 写回答

2条回答 默认 最新

  • douchun3680 2013-02-18 17:50
    关注

    To hide the items out of stock button:

        if (!function_exists('woocommerce_template_loop_add_to_cart')) {
            function woocommerce_template_loop_add_to_cart() {
                global $product;
                if (!$product->is_in_stock()) return;
                woocommerce_get_template('loop/add-to-cart.php');
            }
        }
    

    To change the Add To Cart button:

    // Change the "Add to Cart" button on the single product page.
        function woo_custom_cart_button_text() {
            return __('My Button Text', 'woocommerce'); 
        }
    
        add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text');
    

    Or, if you would specifically like to modify the template structure:

    Create a folder called /woocommerce in your theme's directory > create another folder inside of this called /loop and place the add-to-cart.php file inside. Then modify line 17

    from:

    <p class="out-of-stock" ><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></p>
    

    to:

    <p class="out-of-stock" ><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Out of Stock', 'woocommerce' ) ); ?></p>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序