Also habe ich dies in meiner Funktionsdatei - es definiert die Produkte, die nicht für den kostenlosen Versand berechtigt sind. Es funktioniert alles in Ordnung.
//functions.php
function my_free_shipping( $is_available ) {
global $woocommerce;
// set the product ids that are $product_notfree_ship
$product_notfree_ship = array( '1', '2', '3', '4', '5' );
// get cart contents
$cart_items = $woocommerce->cart->get_cart();
// loop through the items looking for one in the ineligible array
foreach ( $cart_items as $key => $item ) {
if( in_array( $item['product_id'], $product_notfree_ship ) ) {
return false;
}
}
// nothing found return the default value
return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'my_free_shipping', 20 );
Allen Produkt-IDs, die ich in das Array $product_notfree_ship
eingegeben habe, wird der kostenlose Versand verweigert.
Jetzt möchte ich diese Produkt-IDs auf den Produktseiten anrufen, um zu prüfen, ob sie die Meldung "Kostenloser Versand" oder "Zusätzliche Versandkosten" erhalten sollen.
also in meinem theme/woocommerce/single-product/product-image.php (ich will es nach dem main img) file habe ich
//theme/woocommerce/single-product/template.php
$product_notfree_ship = array( '1', '2', '3', '4', '5' );
// this is commented because it didn't work,
// global $product_notfree_ship;
if ( is_single($product_notfree_ship) ) {
echo 'Additional Shipping Charges Apply';
} else {
echo 'FREE SHIPPING on This Product';
}
Nun, das funktioniert, es fühlt sich einfach blöd an, beide Arrays bearbeiten zu müssen, falls eine neue Produkt-ID zum "Nicht versandkostenfreien Produkt-Array" hinzugefügt werden muss.
Also basierend auf der Antwort hier
Ich dachte, wenn global $product_notfree_ship;
vor der if
aufgerufen würde, würde der richtige Code ausgeführt, aber das tat er nicht.
Liegt es daran, dass ich is_single()
verwende? Liegt das daran, dass es sich um ein Array handelt und es anders heißen muss?
Jede Hilfe wird geschätzt. Vielen Dank.
Es ist alles in Ordnung. Sie müssen nur zuerst die Variable global deklarieren, dann können Sie den Wert festlegen und global darauf zugreifen.
function my_free_shipping( $is_available ) {
global $woocommerce, $product_notfree_ship;
// set the product ids that are $product_notfree_ship
$product_notfree_ship = array( '1', '2', '3', '4', '5' );
Dann deklarieren Sie es erneut global, wenn Sie es erneut in einer anderen Datei verwenden
global $product_notfree_ship;
if ( is_single($product_notfree_ship) ) {
echo 'Additional Shipping Charges Apply';
} else {
echo 'FREE SHIPPING on This Product';
}
So funktioniert die globale Variable.
Erkläre es als
global $product_notfree_ship
greifen Sie währenddessen einfach darauf zu
$GLOBALS['product_notfree_ship'];