How do I programatically & DOM (checkout page) remove shipping from a WordPress order if the order contains a specific product type?

huangapple go评论63阅读模式
英文:

How do I programatically & DOM (checkout page) remove shipping from a WordPress order if the order contains a specific product type?

问题

我正在尝试为我的自定义插件执行特殊逻辑。如果用户在购物车中添加了特定产品类型,那么在结算页面上必须有单选框输入,用于确定用户是否希望将特定产品类型发货或存储在保险库中。我已经完成了前端部分(创建单选框输入、构建JavaScript逻辑以删除不必要的DOM等等...),但现在我需要通过编程方式从订单中移除运费,并在结算页面的订单预览中删除"Shipping"行。我尝试了以下过滤器:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_shipping_labels', 10, 2 );
function remove_shipping_labels( $label, $method ) {
    return '';
}

但它只移除了标签文本"Free Shipping",而没有完全删除结算页面订单预览中的整个运费行。如何通过AJAX通过编程方式从订单中移除运费并更新结算页面的用户界面呢?

英文:

I'm trying to do a special logic for my custom plugin. If the user has added a specific product type in their cart, in the checkout page there must be radio inputs that determine whether the user wants the specific product type to be shipped or stored in vault. I've done everything for the frontend part (creating the radio inputs, built the JavaScript logic to remove from the DOM what's not necessary and so on...) but I now need to programatically remove the shipping from the order and remove the "Shipping" row inside the order preview in the checkout page. I tried the following filter

add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_shipping_labels', 10, 2 );
function remove_shipping_labels( $label, $method ) {
    return '';
}

But it's removing just the label text "Free Shipping" but not the entire shipping row inside the order preview in the checkout page. How can I programatically remove the shipping availability from an order through AJAX and update the user interface inside the checkout page?

答案1

得分: 1

function hide_shipping_based_on_prod_type( $rates ) {
	global $woocommerce;
	$items = $woocommerce->cart->get_cart();

	foreach ( $items as $item => $values ) {
		$product_id   = $values['data']->get_id();
		$product_type = WC_Product_Factory::get_product_type( $values['data']->get_id() );

		if ( 'simple' === $product_type ) { //check product types of woocommerce to add more conditions
			unset( $rates['free_shipping:1'] );
		}
	}

	return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_prod_type', 100 );

add_action(
	'after_setup_theme',
	function() {
		WC_Cache_Helper::get_transient_version( 'shipping', true );
	}
);
英文:
function hide_shipping_based_on_prod_type( $rates ) {
    
    	global $woocommerce;
    	$items = $woocommerce->cart->get_cart();
    
    	foreach ( $items as $item => $values ) {
    		$product_id   = $values['data']->get_id();
    		$product_type = WC_Product_Factory::get_product_type( $values['data']->get_id() );
    
    		if ( 'simple' === $product_type ) { //check product types of woocommerce to add more conditions
    			unset( $rates['free_shipping:1'] );
    		}
    	}
    
    	return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_prod_type', 100 );

    add_action(
    	'after_setup_theme',
    	function() {
    		WC_Cache_Helper::get_transient_version( 'shipping', true );
    	}
    );

huangapple
  • 本文由 发表于 2023年2月8日 15:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定