【问题标题】:Code only runs on Woocommerce shop page but not on other Wordpress/Woocomerce pages代码仅在 Woocommerce 商店页面上运行,但不在其他 Wordpress/Woocommerce 页面上运行
【发布时间】:2020-02-13 13:59:55
【问题描述】:

我尝试在所有显示 Woocommerce 产品的 Wordpress 页面中添加数量输入字段。我的代码按预期向所有页面上的所有产品添加了一个数量输入文件,但它只处理 Woocomerce 商店页面上的输入。如果产品在任何其他页面上,包括 Woocommerce 类别页面,则仅显示输入字段,但 id 不会影响任何内容。有人知道我错过了什么吗?谢谢。

/**
 * Add quantity field on the archive page.
 */
function custom_quantity_field_archive() {

    $product = wc_get_product( get_the_ID() );

    if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) {
        woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
    }

}
add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 0, 9 );


/**
 * Add requires JavaScript.
 */
function custom_add_to_cart_quantity_handler() {

    wc_enqueue_js( '
        jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", function() {
            return false;
        });
        jQuery( ".post-type-archive-product" ).on( "change input", ".quantity .qty", function() {
            var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
            // For AJAX add-to-cart actions
            add_to_cart_button.data( "quantity", jQuery( this ).val() );
            // For non-AJAX add-to-cart actions
            add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );
        });
    ' );

}
add_action( 'init', 'custom_add_to_cart_quantity_handler' );

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:

    post-type-archive-product 需要替换为 type-product

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 2021-11-14
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2018-07-01
      • 1970-01-01
      相关资源
      最近更新 更多